package com.mygdx.game.screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.ScreenAdapter;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.utils.viewport.StretchViewport;
import com.mygdx.game.resource.Res;
import com.mygdx.game.stage.ExitConfirmStage;
import com.mygdx.game.stage.GameOverStage;
import com.mygdx.game.stage.HelpStage;
import com.mygdx.game.stage.HomeStage;
import com.mygdx.game.MainGame;
import com.mygdx.game.stage.WelcomeStage;


public class GameScreen extends ScreenAdapter {

    private MainGame mainGame;
    private WelcomeStage welcomeStage;
    private HomeStage homeStage;
    private HelpStage helpStage;
    private ExitConfirmStage exitConfirmStage;
    private GameOverStage gameOverStage;

    public GameScreen(MainGame mainGame) {
        this.mainGame = mainGame;
        init();
    }

    private void init() {
        welcomeStage = new WelcomeStage(mainGame,
                new StretchViewport(
                        mainGame.getWorldWidth(),
                        mainGame.getWorldHeight()
                ));

        welcomeStage.setVisible(true);

        homeStage = new HomeStage(mainGame,
                new StretchViewport(
                        mainGame.getWorldWidth(),
                        mainGame.getWorldHeight())
        );
        helpStage = new HelpStage(mainGame,
                new StretchViewport(
                        mainGame.getWorldWidth(),
                        mainGame.getWorldHeight()
                ));
        exitConfirmStage = new ExitConfirmStage(mainGame,
                new StretchViewport(
                        mainGame.getWorldWidth(),
                        mainGame.getWorldHeight()
                ));

        gameOverStage = new GameOverStage(mainGame,
                new StretchViewport(
                        mainGame.getWorldWidth(),
                        mainGame.getWorldHeight())
                );


        //把输入都先交给欢迎舞台处理
        Gdx.input.setInputProcessor(welcomeStage);
    }

    public void setShowHomeStage(boolean visible){
        homeStage.setVisible(visible);
        Gdx.input.setInputProcessor(homeStage);
    }

    public void setShowHelpStage(boolean visible) {
        helpStage.setVisible(visible);
        if (visible) {
            Gdx.input.setInputProcessor(helpStage);
        } else {
            Gdx.input.setInputProcessor(homeStage);
        }
    }

    public void setShowExitConfirmStage(boolean visible) {
        exitConfirmStage.setVisible(visible);
        if (visible) {
            Gdx.input.setInputProcessor(exitConfirmStage);
        } else {
            Gdx.input.setInputProcessor(homeStage);
        }
    }

    public void setShowGameOverStage(boolean visible){
        gameOverStage.setVisible(visible);
        if (visible){
            Gdx.input.setInputProcessor(gameOverStage);
        }else {
            Gdx.input.setInputProcessor(homeStage);
        }
        gameOverStage.setScore(getHomeStage().getTopGroup().getCurrentScore());
    }

    public HelpStage getHelpStage() {
        return helpStage;
    }

    public HomeStage getHomeStage() {
        return homeStage;
    }

    public ExitConfirmStage getExitConfirmStage() {
        return exitConfirmStage;
    }

    public GameOverStage getGameOverStage() {
        return gameOverStage;
    }

    @Override
    public void render(float delta) {

        Gdx.gl.glClearColor(Res.BG_RGBA.r, Res.BG_RGBA.g, Res.BG_RGBA.b, Res.BG_RGBA.a);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        if (welcomeStage.getVisible()){
            welcomeStage.act();
            welcomeStage.draw();
        }

        if (homeStage.getVisible()) {
            //主舞台必须一直绘制
            homeStage.act();
            homeStage.draw();
        }

        if (helpStage.getVisible()) {
            helpStage.act();
            helpStage.draw();
        }

        if (exitConfirmStage.getVisible()) {
            exitConfirmStage.act();
            exitConfirmStage.draw();
        }

        if (gameOverStage.getVisible()){
            gameOverStage.act();
            gameOverStage.draw();
        }
    }

    @Override
    public void dispose() {
        //需要手动销毁舞台
        if (homeStage != null) {
            homeStage.dispose();
        }
        if (helpStage != null){
            helpStage.dispose();
        }
        if (gameOverStage != null){
            gameOverStage.dispose();
        }
        if (exitConfirmStage != null){
            exitConfirmStage.dispose();
        }
    }
}
